import java.util.ArrayList;
import java.util.HashMap;

public class InitClass {
    //key是网段 value是该网段的设备
    HashMap<String, ArrayList<NetNode>> segMap = new HashMap<>();
    //key 是mac地址 需要根据mac地址找到起始主机，触发起始主机的transferFrame
    HashMap<String, Host> hosts = new HashMap<>();
    ArrayList<Exchange> exchanges = new ArrayList<>();


    public  void init(int exchangeNum,int hostNum){
        //生成exchangeNum+1个网段，一个网段包括一些主机和网桥
        for (int i = 0; i < exchangeNum; i++) {
                String netSeg0 = "192.168.1."+i;
                int g = i + 1;
                String netSeg1 = "192.168.1."+g;
                Exchange exchange = new Exchange();
                exchange.setName("e"+i);
                exchange.setlNetSeg(netSeg0);
                exchange.setrNetSeg(netSeg1);
                exchanges.add(exchange);
                //网桥左网段对应的设备列表
                ArrayList<NetNode> lnodes = segMap.get(netSeg0);
                if (lnodes == null){
                    lnodes = new ArrayList<>();
                }
                //网桥右网段对应的设备列表
                ArrayList<NetNode> rnodes = segMap.get(netSeg1);
                if (rnodes == null){
                    rnodes = new ArrayList<>();
                }
                lnodes.add(exchange);
                rnodes.add(exchange);
                int mid = hostNum/2;
                //一半主机加入网桥左边端口的网段，即netSeg0这个网段
                for (int j = 0; j < mid; j++) {
                    Host host = new Host("e"+i+'h'+j);
                    host.setNetSeg(netSeg0);
                    lnodes.add(host);
                    hosts.put(host.getMac(),host);
                }
                //一半主机加入网桥右边端口的网段 即netSeg1这个网段 hostNum奇数时右边多一个主机
                for (int j = mid; j < hostNum; j++) {
                    Host host = new Host("e"+i+'h'+j);
                    host.setNetSeg(netSeg1);
                    rnodes.add(host);
                    hosts.put(host.getMac(),host);
                }
                //每一个网段，对应有哪些设备配置好了
                segMap.put(netSeg0,lnodes);
                segMap.put(netSeg1,rnodes);
        }
        for (Exchange exchange : exchanges) {
            //找到网桥左边网段的所有设备
            ArrayList<NetNode> lnodes = segMap.get(exchange.getlNetSeg());
            ArrayList<NetNode> arr0 = new ArrayList<>();
            ArrayList<NetNode> arr1 = new ArrayList<>();
            for (NetNode lnode : lnodes) {
                //除去该网桥自己
                if (!lnode.getId().equals(exchange.getId())){
                    arr0.add(lnode);
                }
            }
            //配置网桥左边网段的设备
            exchange.setLeftPortNodes(arr0);
            ArrayList<NetNode> rnodes = segMap.get(exchange.getrNetSeg());
            for (NetNode rnode : rnodes) {
                if (!rnode.getId().equals(exchange.getId())){
                    arr1.add(rnode);
                }
            }
            //配置网桥右边网段的设备
            exchange.setRightPortNodes(arr1);
        }
        for (String s : hosts.keySet()) {
            Host host = hosts.get(s);
            ArrayList<NetNode> nodes = segMap.get(host.getNetSeg());
            ArrayList<NetNode> arr0 = new ArrayList<>();
            for (NetNode node : nodes) {
                if (!node.getId().equals(host.getId())){
                    arr0.add(node);
                }
            }
            host.setNodes(arr0);
        }
        }
    }


